<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 聚合接口控制器
 */
class Combination extends ApiBase
{
    
    /**
     * 首页接口
     */
    public function index()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['category_id', 'page', 'limit']);
        
        $article_category_list = $this->logicArticle->getArticleCategoryList();
        $article_list          = $this->logicArticle->getArticleList($filteredData);
        
        return $this->apiReturn(compact('article_category_list', 'article_list'));
    }
    
    /**
     * 获取首页聚合数据
     */
    public function getHomePageData()
    {
        // 获取热门商品
        $hot_products = $this->logicProduct->getHotProducts([]);

        // 获取广告
        $advertisements = $this->logicAdvertisement->getAdvertisementList(['position'=>'banner']);

        // 获取公告
        $announcements = $this->logicAnnouncement->getAnnouncementList([]);
        
        return $this->apiReturn(compact('hot_products', 'advertisements', 'announcements'));
    }
    
    /**
     * 详情接口
     */
    public function details()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['id']);
        
        $article_category_list = $this->logicArticle->getArticleCategoryList();
        $article_details       = $this->logicArticle->getArticleInfo($filteredData);
        
        return $this->apiReturn(compact('article_category_list', 'article_details'));
    }
    
    /**
     * 过滤API参数，只保留指定的业务字段
     */
    private function filterApiParams($data, $allowedFields)
    {
        $filtered = [];
        foreach ($allowedFields as $field) {
            if (isset($data[$field])) {
                $filtered[$field] = $data[$field];
            }
        }
        return $filtered;
    }
}
